package main

import (
	"BlendSDF/blend"
	"BlendSDF/save"
	"BlendSDF/sdf"
	"fmt"
	"image"
	"image/color"
	"sync"
)

var wg sync.WaitGroup

func main() {
	url := "./sample/%d.png"
	//样本数
	n := 10
	//获取长和宽
	sdfs := make([][][]float64, n)
	length, width, _ := sdf.GetLengthAndWidth(fmt.Sprintf(url, 0))
	var err error
	//计算有向距离场
	for i := 0; i <= n-1; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			photoSDF, err := sdf.GetSDF(fmt.Sprintf(url, i))
			if err != nil {
				fmt.Println(err)
			}
			sdfs[i] = photoSDF
		}(i)
	}

	resultImg := image.NewRGBA(image.Rect(0, 0, length, width))
	wg.Add(1)
	go func(img *image.RGBA) {
		defer wg.Done()
		for i := 0; i < width; i++ {
			for j := 0; j < length; j++ {
				resultImg.Set(j, i, color.White)
			}
		}
	}(resultImg)
	//同步
	wg.Wait()

	if err != nil {
		return
	}
	//混合
	for i := 1; i <= n-1; i++ {
		blend.BlendSDFToAlpha(sdfs[i-1], sdfs[i], length, width, i, len(sdfs), resultImg)
	}
	//保存
	err = save.SaveInPng("./demo.png", resultImg)
	if err != nil {
		fmt.Println(err)
	}
}
